package kr.hyosang.android.penfo;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public class BoardBase<ResultData> extends Activity {
	LoaderListener<ResultData> listener = null;
	private int nPage = 1;
	protected String boardId;
	private String boardNo;
	public void refresh() {
	    //상속클래스에서 구현 필요
	}
	
	public void setNo(String no) {
		boardNo = no;
	}
	
	public String getNo() {
	    return boardNo;
	}
	
	public void setPage(String p) {
		try {
			nPage = Integer.parseInt(p);
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}
	}
	
	public int getPage() {
		return nPage;
	}
	
	public String getPrevPageURL() {
		if(nPage > 2) {
			nPage--;
			return getLoadURL(true);
		}
		
		return null;
	}
			
	
	public String getNextPageURL() {
		nPage++;
		return getLoadURL(true);
	}
	
	public String getLoadURL() {
		try {
			Integer.parseInt(boardNo);
			return getLoadURL(false);
		}catch(NumberFormatException e) {
			return getLoadURL(true);
		}
	}
	
	public String getLoadURL(boolean bOnlyList) {
		if(bOnlyList) {
			return String.format("%s?id=%s&page=%d", Definition.URL.BOARD, boardId, nPage);
		}else {
			return String.format("%s?id=%s&no=%s&page=%d", Definition.URL.BOARD, boardId, boardNo, nPage);
		}
	}
	
	public void setLoaderListener(LoaderListener<ResultData> l) {
		listener = l;
	}
	
	static interface LoaderListener<ResultData> {
		public void onComplete(ResultData data);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(0, Definition.MENU_ID.LOGOUT, 0, R.string.logout);
        menu.add(0, Definition.MENU_ID.LOGIN, 0, R.string.login);
        menu.add(0, Definition.MENU_ID.REFRESH, 0, R.string.refresh);
	    
	    return super.onCreateOptionsMenu(menu);
	}
	
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean bLogin = SharedObject.getInstance().bLogin;
        
        menu.findItem(Definition.MENU_ID.LOGIN).setEnabled(!bLogin);
        menu.findItem(Definition.MENU_ID.LOGOUT).setEnabled(bLogin);
        
        return super.onPrepareOptionsMenu(menu);
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == Definition.MENU_ID.LOGIN) {
            //로그인 액티비티 시작
            Intent i = new Intent(this, LoginActivity.class);
            startActivityForResult(i, Definition.ACTIVITY_LOGIN);
        }else if(item.getItemId() == Definition.MENU_ID.LOGOUT) {
            //로그아웃 => ZBSESSIONID 삭제하고 새로고침
            SharedObject.getInstance().logout();
            refresh();
        }else if(item.getItemId() == Definition.MENU_ID.REFRESH) {
            refresh();
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if(requestCode == Definition.ACTIVITY_LOGIN) {
            if(resultCode == Definition.RESULT_OK) {
                refresh();
            }
        }
    }  

}
